// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import FSMManager from "../FSM/FSMManager";
import Walk from "../playerState/walk";
import Idle from "../playerState/idle";
import MessageCenter from "../MessageCenter/MessageCenter";
import {MessageType} from "../MessageCenter/Message";
import Input from "../common/Input";

const {ccclass, property} = cc._decorator;

enum PlayerState {
    IDLE,
    WALK,
    WORK
}

@ccclass
export default class PlayerControl extends cc.Component {
    // 状态机
    fsmManager: FSMManager;

    onLoad() {}

    start() {
        this.fsmManager = new FSMManager();
        // 创建两个状态
        let walk = new Walk(PlayerState.WALK, this, this.fsmManager);
        let idle = new Idle(PlayerState.IDLE, this, this.fsmManager);
        // 注意！这里次序要跟枚举一致！
        this.fsmManager.stateList = [idle, walk];
        // 切换默认状态
        this.fsmManager.changeState(PlayerState.IDLE);
    }


    update(dt) {
        if (this.fsmManager.currentState != -1) {
            // 判断状态
            if (Input.Instance.horizontal == 0 && Input.Instance.vertical == 0) {
                this.fsmManager.changeState(PlayerState.IDLE);
            } else {
                this.fsmManager.changeState(PlayerState.WALK);
            }

            this.fsmManager.onUpdate(dt);
        }
    }

    // 碰撞检测
    onBeginContact(contact, self, others) {
        if (others.tag == 1) {
            // 拾取物品
            console.log(`碰撞成功，发送消息`)
            MessageCenter.SendCustomMessage(MessageType.TYPE_UI, MessageType.UI_CHANGE_PACKAGE, others)
        }
    }
}
